Skip to content

Fix pick channels crash when _orig_units is None (#11314)#14006

Open
CedricConday wants to merge 4 commits into
mne-tools:mainfrom
CedricConday:fix/pick-channels-orig-units-none
Open

Fix pick channels crash when _orig_units is None (#11314)#14006
CedricConday wants to merge 4 commits into
mne-tools:mainfrom
CedricConday:fix/pick-channels-orig-units-none

Conversation

@CedricConday

@CedricConday CedricConday commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #11314

What was wrong

When picking/reordering channels on a Raw, the code rebuilds _orig_units:

self._orig_units = {k: v for k, v in self._orig_units.items() if k in self.ch_names}

If self._orig_units is None (which happens with some readers and RawArray flows), this raises AttributeError: 'NoneType' object has no attribute 'items'. The pick itself otherwise succeeds.

Fix

Only rebuild _orig_units when it is truthy; leave None/empty as-is.

Test

Adds test_pick_channels_orig_units_none: picking with _orig_units = None no longer raises. Verified it raises AttributeError on main and passes with this change.


AI-assisted, human-reviewed — I'm an AI engineer; I find, fix, and test with AI (Claude Code), then review and verify before opening.

@larsoner larsoner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM

Comment thread mne/channels/tests/test_channels.py Outdated
Comment on lines +703 to +705
import numpy as np

import mne

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to nest these they're required by MNE

Picking/reordering channels built {k: v for ... in self._orig_units.items()}
unconditionally, raising AttributeError when _orig_units is None (as produced
by some readers / RawArray flows). Guard for a falsy _orig_units. Adds a test.
Comment thread mne/channels/tests/test_channels.py Outdated
Comment on lines +704 to +705
info = mne.create_info(["Fp1", "Fp2", "F3", "F4"], 100.0, "eeg")
raw = mne.io.RawArray(np.zeros((4, 100)), info)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create_info and RawArray are already imported directly at top of file

Suggested change
info = mne.create_info(["Fp1", "Fp2", "F3", "F4"], 100.0, "eeg")
raw = mne.io.RawArray(np.zeros((4, 100)), info)
info = create_info(["Fp1", "Fp2", "F3", "F4"], 100.0, "eeg")
raw = RawArray(np.zeros((4, 100)), info)

Comment thread mne/channels/tests/test_channels.py Outdated
from numpy.testing import assert_allclose, assert_array_equal, assert_equal
from scipy.io import savemat

import mne

@drammock drammock Jul 10, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary; the two API items you use (RawArray and create_info) are already being imported directly, just a few lines down.

Address @drammock review on mne-tools#14006: create_info and RawArray are already
imported directly at the top of the test module, so call them directly and
remove the now-unused `import mne`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LyuFNWN45FNffpGwsC4Su7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pick_Channels Returns AttributeError: 'NoneType' object has no attribute 'items

3 participants